#include "utils.hpp"
#include "level.hpp"
#include "massage.hpp"
#include "format.hpp"
#include "logSink.hpp"
#include "buffer.hpp"
#include <string>
#include "log.hpp"


void test_log()
{
    wzh::Logger::ptr logger = wzh::LoggerManager::getInstance().getLogger("async_logger");
    logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger->info(__FILE__, __LINE__, "%s", "测试日志");
    logger->error(__FILE__, __LINE__, "%s", "测试日志");
    logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
    size_t count = 0;
    while(count < 500000)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    }
}

int main()
{

    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if(ifs.is_open() == false)
    // {
    //     std::cout << "open faild\n";
    //     return -1;
    // }

    // ifs.seekg(0, std::ios::end); //跳转到末尾
    // size_t fsize = ifs.tellg();
    // ifs.seekg(0, std::ios::beg); //跳转到开头
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);
    // if(ifs.good() == false)
    // {
    //     std::cout << "read error\n";
    //     return -1;
    // }
    // ifs.close();

    // std::cout << fsize << std::endl;

    // wzh::Buffer buffer;
    // for(int i = 0; i < body.size(); i++)
    // {
    //     buffer.push(&body[i], 1);
    // }
    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // if(ofs.is_open() == false) 
    // {
    //     std::cout << "open faild\n";
    // }
    // size_t rsize = buffer.readAbleSize();
    // for(int i = 0; i < rsize; i++)
    // {
    //     ofs.write(buffer.begin(), 1);
    //     if(ofs.good() == false) 
    //     {
    //         std::cout << "write faild\n";
    //         return -1;
    //     }
    //     buffer.moveRead(1);
    // }
    // ofs.close();

    // ofs.write(buffer.begin(), buffer.readAbleSize());

    // std::string logger_name = "sync_logger";
    // wzh::LogLevel::values limit = wzh::LogLevel::values::WARN;
    // wzh::Formatter::ptr fmt(new wzh::Formatter("[%d{%H:%M:%S}][%c][&f:%l][%p]%T%m%n"));
    // wzh::LogSink::ptr stdout_lsp = wzh::SinkFactory::create<wzh::StdoutSink>();
    // wzh::LogSink::ptr file_lsp = wzh::SinkFactory::create<wzh::FileSink>("./logfile/test.log");
    // wzh::LogSink::ptr roll_lsp = wzh::SinkFactory::create<wzh::RollSink_Size>("./logfile/roll-", 1024*1024);
    // std::vector<wzh::LogSink::ptr> sinks = {stdout_lsp, file_lsp, roll_lsp};
    // wzh::Logger::ptr logger(new wzh::SyncLogger(logger_name, limit, fmt, sinks));

    std::unique_ptr<wzh::LoggerBuilder> builder(new wzh::LocalLoggerBuilder());
    builder->buildLoggerLevel(wzh::LogLevel::values::WARN);
    builder->buildLoggerName("async_logger");
    builder->buildFormatter("[%c]%m%n");
    builder->buildLoggerType(wzh::LoggerType::LOGGER_ASYNC);
    builder->buildEnableUnsafeAsync();
    builder->buildSink<wzh::FileSink>("./logfile/async.log");
    builder->buildSink<wzh::StdoutSink>();
    builder->build();
    test_log();

    // wzh::LogMag msg(wzh::LogLevel::values::INFO, 52, "main.c", "root", "格式化功能测试...");
    // wzh::Formatter fmt("abc%%ab%%c[%d{%H:%M:%S}]%m%n");
    // std::string str = fmt.format(msg);

    // wzh::LogSink::ptr stdout_lsp = wzh::SinkFactory::create<wzh::StdoutSink>();
    // wzh::LogSink::ptr file_lsp = wzh::SinkFactory::create<wzh::FileSink>("./logfile/test.log");
    // wzh::LogSink::ptr roll_lsp = wzh::SinkFactory::create<wzh::RollSink_Size>("./logfile/roll-", 1024 * 1024);

    // stdout_lsp->log(str.c_str(), str.size());
    // file_lsp->log(str.c_str(), str.size());
    // size_t cursize = 0;
    // size_t count = 0;
    // while (cursize < 1024 * 1024 * 10)
    // {
    //     std::string tmp = str + std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(), tmp.size());
    //     cursize += tmp.size();
    // }

    // wzh::LogMag msg;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::DEBUG) << std::endl;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::INFO) << std::endl;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::ERROR) << std::endl;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::FATAL) << std::endl;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::OFF) << std::endl;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::UNKNOW) << std::endl;
    // std::cout << wzh::LogLevel::toString(wzh::LogLevel::values::WARN) << std::endl;
    // std::cout << wzh::util::Date::nowTime() << std::endl;
    // std::string pathname = "./abc/bcd/a.txt";
    // wzh::util::File::createDirectory(wzh::util::File::path(pathname));
    return 0;
}

